package view.manage;

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JPasswordField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import main.WinkelApplication;
import model.User;
import model.Klant;

/**
 *
 * @author JJ Zeilmaker
 */
/*
 * 
 *   
    
 */
public class Login extends JPanel implements MouseListener, ActionListener {

    private final int verticalPosition = 0;
    private final int formOffset = 30;
    private JTextField tfUsrName;
    private JTextField tfUsrPwd;

    public Login() {
        super();
        this.setLayout(null);
        initComponents();
    }

    private void initComponents() {
        addBackBtn();
        addTitle();
        addForm();
    }

    private void addTitle() {
        JLabel lblTitle1 = new JLabel();
        lblTitle1.setText("Log in");
        lblTitle1.setBounds(20, 20, 150, 20);
        lblTitle1.setFont(WinkelApplication.FONT_16_BOLD);
        add(lblTitle1);
    }

    /**
     * Show forum fields to login on screen
     * 
     */
    private void addForm() {
        JLabel lblNaam = new JLabel("Gebruikersnaam:");
        lblNaam.setBounds(20, verticalPosition + (formOffset * 2), 100, 20);
        lblNaam.setFont(WinkelApplication.FONT_10_BOLD);
        add(lblNaam);

        tfUsrName = new JTextField();
        tfUsrName.setBounds(120, verticalPosition + (formOffset * 2), 130, 20);
        tfUsrName.setFont(WinkelApplication.FONT_10_BOLD);
        add(tfUsrName);

        JLabel lblAddress = new JLabel("Wachtwoord:");
        lblAddress.setBounds(20, verticalPosition + (formOffset * 3), 100, 20);
        lblAddress.setFont(WinkelApplication.FONT_10_BOLD);
        add(lblAddress);

        tfUsrPwd = new JPasswordField();
        tfUsrPwd.setBounds(120, verticalPosition + (formOffset * 3), 130, 20);
        tfUsrPwd.setFont(WinkelApplication.FONT_10_BOLD);
        add(tfUsrPwd);

        JButton btnSend = new JButton("Login");
        btnSend.setBounds(120, verticalPosition + (formOffset * 4), 130, 20);
        btnSend.setFont(WinkelApplication.FONT_10_BOLD);
        btnSend.addActionListener(this);
        this.add(btnSend);
        
  
    }    

    /*
     * Add Back button to Application
     */
    private void addBackBtn(){
        JButton btnSend = new JButton("Terug");
        btnSend.setBounds(662, 420, 75, 20);
        btnSend.setFont(WinkelApplication.FONT_10_BOLD);
        btnSend.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ backBtn(); } }); 
        add(btnSend);
    }    
    
    public void backBtn(){
       WinkelApplication.getInstance().showPanel(new view.CategoryList());
    }
    /**
     * When login butten pressed do next action
     * Check by posted userName and usrPwd if thay exist
     * if exist check gebruiker_rolId if klant log klant in and go back to category screen (set Klant)
     * if exist check gebruiker_rolId if beheerder go to beheer screen. (set User)
     * @param event 
     */
    @Override
    public void actionPerformed(ActionEvent event) {
        String usrName = tfUsrName.getText();
        String usrPwd = tfUsrPwd.getText();
        
        User user = WinkelApplication.getQueryManager().getUser(usrName, usrPwd);
        
        
        if(usrName.equals(user.getGebruikersnaam()) & usrPwd.equals(user.getWachtwoord())){
            WinkelApplication.getInstance().setUser(user.getGebruiker_id());
            if(user.getRol_id() == 1){
                     Klant klant = WinkelApplication.getQueryManager().getKlant(user.getGebruiker_id());
                     WinkelApplication.getInstance().setKlant(user.getGebruiker_id());
                              
                     WinkelApplication.getInstance().showPanel(new view.CategoryList());        
            }else{
            WinkelApplication.window.newWindow(new view.manage.Main());            
            }
        }else{
            JOptionPane.showMessageDialog(WinkelApplication.getInstance().getMainWindow(), "Inloggen mislukt. Probeer het opnieuw.", "Error", JOptionPane.ERROR_MESSAGE);
        }

 
    }

    @Override
    public void mouseClicked(MouseEvent event) {
        // the user clicked on the title, go back to the first screen
        //WinkelApplication.getInstance().showPanel(new view.CategoryList());
    }

    @Override
    public void mouseEntered(MouseEvent event) {
        // Intentionally left blank.
    }

    @Override
    public void mousePressed(MouseEvent event) {
        // Intentionally left blank.
    }

    @Override
    public void mouseReleased(MouseEvent event) {
        // Intentionally left blank.
    }

    @Override
    public void mouseExited(MouseEvent event) {
        // Intentionally left blank.
    }

    @Override
    public void paint(Graphics graphics) {
        super.paint(graphics);
        graphics.drawLine(20, 45, 700, 45);		// under H Title
    }
}
